<template>
  <div class="trainer-detail-page" v-if="trainer">
    <div class="container">
      <!-- Header Section -->
      <div class="trainer-header">
        <div class="trainer-basic-info">
          <div class="trainer-avatar-large">
            <img :src="trainer.avatar" :alt="trainer.name" />
            <div class="availability-status" :class="trainer.availability">
              {{ trainer.availability === 'available' ? '可预约' : '忙碌中' }}
            </div>
          </div>
          
          <div class="trainer-main-info">
            <h1 class="trainer-name">{{ trainer.name }}</h1>
            <div class="trainer-rating-large">
              <span class="stars">{{ '★'.repeat(Math.floor(trainer.rating)) }}</span>
              <span class="rating-score">{{ trainer.rating }}</span>
              <span class="review-count">({{ trainer.reviewCount }}条评价)</span>
            </div>
            
            <div class="trainer-tags">
              <span 
                v-for="specialty in trainer.specialties" 
                :key="specialty"
                class="specialty-tag-large"
              >
                {{ specialty }}
              </span>
            </div>
            
            <div class="trainer-quick-stats">
              <div class="stat-item">
                <span class="stat-label">经验</span>
                <span class="stat-value">{{ trainer.experience }}年</span>
              </div>
              <div class="stat-item">
                <span class="stat-label">价格</span>
                <span class="stat-value">¥{{ trainer.price }}/课时</span>
              </div>
              <div class="stat-item">
                <span class="stat-label">地区</span>
                <span class="stat-value">{{ trainer.city }}</span>
              </div>
              <div class="stat-item">
                <span class="stat-label">学员</span>
                <span class="stat-value">{{ trainer.studentCount }}+</span>
              </div>
            </div>
          </div>
        </div>
        
        <div class="trainer-actions-panel">
          <button class="btn btn-primary btn-large" @click="bookTrainer">
            立即预约
          </button>
          <button class="btn btn-secondary" @click="addToFavorites">
            {{ isFavorite ? '已收藏' : '收藏' }}
          </button>
          <button class="btn btn-outline" @click="shareTrainer">
            分享
          </button>
        </div>
      </div>

      <!-- Content Tabs -->
      <div class="content-tabs">
        <div class="tab-nav">
          <button 
            v-for="tab in tabs" 
            :key="tab.id"
            :class="['tab-btn', { active: activeTab === tab.id }]"
            @click="activeTab = tab.id"
          >
            {{ tab.label }}
          </button>
        </div>

        <!-- Tab Content -->
        <div class="tab-content">
          <!-- 个人简介 -->
          <div v-if="activeTab === 'profile'" class="tab-panel">
            <div class="profile-section">
              <h3>个人简介</h3>
              <p class="bio-text">{{ trainer.bio }}</p>
              
              <h3>专业背景</h3>
              <div class="background-info">
                <div class="info-item">
                  <strong>教育背景：</strong>{{ trainer.education }}
                </div>
                <div class="info-item">
                  <strong>从业时间：</strong>{{ trainer.experience }}年
                </div>
                <div class="info-item">
                  <strong>专业领域：</strong>{{ trainer.specialties.join('、') }}
                </div>
              </div>
              
              <h3>认证资质</h3>
              <div class="certifications-grid">
                <div 
                  v-for="cert in trainer.detailedCertifications" 
                  :key="cert.name"
                  class="cert-card"
                >
                  <div class="cert-name">{{ cert.name }}</div>
                  <div class="cert-date">获得时间：{{ cert.date }}</div>
                  <div class="cert-org">颁发机构：{{ cert.organization }}</div>
                </div>
              </div>
            </div>
          </div>

          <!-- 教学案例 -->
          <div v-if="activeTab === 'cases'" class="tab-panel">
            <div class="cases-section">
              <h3>成功案例</h3>
              <div class="cases-grid">
                <div 
                  v-for="case_ in trainer.cases" 
                  :key="case_.id"
                  class="case-card"
                >
                  <div class="case-images">
                    <img :src="case_.beforeImage" alt="训练前" class="case-image">
                    <div class="arrow">→</div>
                    <img :src="case_.afterImage" alt="训练后" class="case-image">
                  </div>
                  <div class="case-info">
                    <h4>{{ case_.title }}</h4>
                    <p class="case-description">{{ case_.description }}</p>
                    <div class="case-stats">
                      <span>训练周期：{{ case_.duration }}</span>
                      <span>训练频次：{{ case_.frequency }}</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- 训练风格 -->
          <div v-if="activeTab === 'style'" class="tab-panel">
            <div class="style-section">
              <h3>训练风格</h3>
              <div class="style-description">
                <p>{{ trainer.trainingStyle.description }}</p>
              </div>
              
              <h3>训练特色</h3>
              <div class="features-grid">
                <div 
                  v-for="feature in trainer.trainingStyle.features" 
                  :key="feature"
                  class="feature-item"
                >
                  <span class="feature-icon">✓</span>
                  <span class="feature-text">{{ feature }}</span>
                </div>
              </div>
              
              <h3>适合人群</h3>
              <div class="target-audience">
                <div 
                  v-for="audience in trainer.trainingStyle.targetAudience" 
                  :key="audience"
                  class="audience-tag"
                >
                  {{ audience }}
                </div>
              </div>
            </div>
          </div>

          <!-- 时间安排 -->
          <div v-if="activeTab === 'schedule'" class="tab-panel">
            <div class="schedule-section">
              <h3>可预约时间</h3>
              <div class="calendar-widget">
                <div class="calendar-header">
                  <button @click="previousWeek" class="nav-btn">‹</button>
                  <span class="week-range">{{ currentWeekRange }}</span>
                  <button @click="nextWeek" class="nav-btn">›</button>
                </div>
                
                <div class="calendar-grid">
                  <div class="day-header">时间</div>
                  <div 
                    v-for="day in weekDays" 
                    :key="day.date"
                    class="day-header"
                  >
                    {{ day.name }}<br>{{ day.date }}
                  </div>
                  
                  <div 
                    v-for="time in timeSlots" 
                    :key="time"
                    class="time-slot"
                  >
                    {{ time }}
                  </div>
                  
                  <div 
                    v-for="day in weekDays" 
                    :key="`${day.date}-slots`"
                    class="day-slots"
                  >
                    <div 
                      v-for="time in timeSlots" 
                      :key="`${day.date}-${time}`"
                      :class="['slot', getSlotStatus(day.date, time)]"
                      @click="selectSlot(day.date, time)"
                    >
                      {{ getSlotStatus(day.date, time) === 'available' ? '可约' : 
                         getSlotStatus(day.date, time) === 'booked' ? '已约' : '休息' }}
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- 用户评价 -->
          <div v-if="activeTab === 'reviews'" class="tab-panel">
            <div class="reviews-section">
              <div class="reviews-summary">
                <div class="rating-overview">
                  <div class="overall-rating">
                    <span class="rating-number">{{ trainer.rating }}</span>
                    <div class="rating-stars">{{ '★'.repeat(Math.floor(trainer.rating)) }}</div>
                    <div class="rating-count">基于{{ trainer.reviewCount }}条评价</div>
                  </div>
                  
                  <div class="rating-breakdown">
                    <div 
                      v-for="(count, stars) in trainer.ratingBreakdown" 
                      :key="stars"
                      class="rating-bar"
                    >
                      <span class="stars-label">{{ stars }}星</span>
                      <div class="bar-container">
                        <div 
                          class="bar-fill" 
                          :style="{ width: (count / trainer.reviewCount * 100) + '%' }"
                        ></div>
                      </div>
                      <span class="count">{{ count }}</span>
                    </div>
                  </div>
                </div>
              </div>
              
              <div class="reviews-list">
                <div 
                  v-for="review in trainer.reviews" 
                  :key="review.id"
                  class="review-card"
                >
                  <div class="review-header">
                    <div class="reviewer-info">
                      <img :src="review.avatar" :alt="review.name" class="reviewer-avatar">
                      <div>
                        <div class="reviewer-name">{{ review.name }}</div>
                        <div class="review-date">{{ review.date }}</div>
                      </div>
                    </div>
                    <div class="review-rating">
                      {{ '★'.repeat(review.rating) }}
                    </div>
                  </div>
                  <div class="review-content">
                    <p>{{ review.content }}</p>
                  </div>
                  <div class="review-tags" v-if="review.tags">
                    <span 
                      v-for="tag in review.tags" 
                      :key="tag"
                      class="review-tag"
                    >
                      {{ tag }}
                    </span>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()

// Reactive data
const trainer = ref(null)
const activeTab = ref('profile')
const isFavorite = ref(false)
const currentWeek = ref(new Date())

const tabs = ref([
  { id: 'profile', label: '个人简介' },
  { id: 'cases', label: '教学案例' },
  { id: 'style', label: '训练风格' },
  { id: 'schedule', label: '时间安排' },
  { id: 'reviews', label: '用户评价' }
])

const timeSlots = ref([
  '09:00', '10:00', '11:00', '14:00', '15:00', '16:00', '17:00', '18:00', '19:00', '20:00'
])

// Mock detailed trainer data
const mockTrainerData = {
  1: {
    id: 1,
    name: '张健',
    avatar: 'https://via.placeholder.com/200/667eea/ffffff?text=张健',
    rating: 4.9,
    reviewCount: 128,
    specialties: ['增肌', '力量训练'],
    experience: 5,
    price: 350,
    city: '北京',
    certifications: ['ACSM', 'NASM'],
    availability: 'available',
    bio: '拥有5年专业健身指导经验，专注于力量训练和增肌指导。曾帮助200+学员达成健身目标，擅长制定个性化训练计划。',
    education: '北京体育大学运动训练专业',
    studentCount: 200,
    detailedCertifications: [
      { name: 'ACSM认证私人教练', date: '2019-03', organization: '美国运动医学学会' },
      { name: 'NASM纠正性运动专家', date: '2020-06', organization: '美国国家运动医学学会' }
    ],
    cases: [
      {
        id: 1,
        title: '3个月增肌15斤',
        description: '通过科学的力量训练和营养指导，帮助学员在3个月内增加15斤纯肌肉。',
        beforeImage: 'https://via.placeholder.com/150/cccccc/666666?text=训练前',
        afterImage: 'https://via.placeholder.com/150/48bb78/ffffff?text=训练后',
        duration: '3个月',
        frequency: '每周4次'
      }
    ],
    trainingStyle: {
      description: '注重基础动作的标准性，循序渐进地提升训练强度。善于激励学员，营造积极的训练氛围。',
      features: ['动作标准化教学', '个性化训练计划', '营养指导', '心理激励'],
      targetAudience: ['健身新手', '增肌需求者', '力量提升者']
    },
    ratingBreakdown: {
      5: 98,
      4: 25,
      3: 4,
      2: 1,
      1: 0
    },
    reviews: [
      {
        id: 1,
        name: '李同学',
        avatar: 'https://via.placeholder.com/40/4299e1/ffffff?text=李',
        rating: 5,
        date: '2024-01-15',
        content: '张教练非常专业，动作指导很到位，3个月下来效果明显！',
        tags: ['专业', '耐心', '效果好']
      }
    ]
  }
}

// Computed properties
const currentWeekRange = computed(() => {
  const start = new Date(currentWeek.value)
  const end = new Date(start)
  end.setDate(start.getDate() + 6)
  return `${start.getMonth() + 1}月${start.getDate()}日 - ${end.getMonth() + 1}月${end.getDate()}日`
})

const weekDays = computed(() => {
  const days = []
  const start = new Date(currentWeek.value)
  
  for (let i = 0; i < 7; i++) {
    const date = new Date(start)
    date.setDate(start.getDate() + i)
    days.push({
      name: ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][date.getDay()],
      date: `${date.getMonth() + 1}/${date.getDate()}`
    })
  }
  
  return days
})

// Methods
const bookTrainer = () => {
  router.push(`/booking?trainerId=${trainer.value.id}`)
}

const addToFavorites = () => {
  isFavorite.value = !isFavorite.value
}

const shareTrainer = () => {
  // Share functionality
  alert('分享功能')
}

const previousWeek = () => {
  const newDate = new Date(currentWeek.value)
  newDate.setDate(newDate.getDate() - 7)
  currentWeek.value = newDate
}

const nextWeek = () => {
  const newDate = new Date(currentWeek.value)
  newDate.setDate(newDate.getDate() + 7)
  currentWeek.value = newDate
}

const getSlotStatus = (date, time) => {
  // Mock availability logic
  const random = Math.random()
  if (random > 0.7) return 'available'
  if (random > 0.4) return 'booked'
  return 'unavailable'
}

const selectSlot = (date, time) => {
  if (getSlotStatus(date, time) === 'available') {
    router.push(`/booking?trainerId=${trainer.value.id}&date=${date}&time=${time}`)
  }
}

onMounted(() => {
  const trainerId = parseInt(route.params.id)
  trainer.value = mockTrainerData[trainerId]
  
  if (!trainer.value) {
    router.push('/trainers')
  }
})
</script>

<style scoped>
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

/* Header Section */
.trainer-header {
  background: white;
  border-radius: 15px;
  padding: 2rem;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
  margin-bottom: 2rem;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2rem;
  align-items: start;
}

.trainer-basic-info {
  display: flex;
  gap: 2rem;
  align-items: start;
}

.trainer-avatar-large {
  position: relative;
}

.trainer-avatar-large img {
  width: 150px;
  height: 150px;
  border-radius: 50%;
  object-fit: cover;
}

.availability-status {
  position: absolute;
  bottom: 10px;
  right: 10px;
  padding: 0.5rem 1rem;
  border-radius: 20px;
  font-size: 0.8rem;
  font-weight: 600;
}

.availability-status.available {
  background: #48bb78;
  color: white;
}

.availability-status.busy {
  background: #ed8936;
  color: white;
}

.trainer-main-info {
  flex: 1;
}

.trainer-name {
  font-size: 2.5rem;
  font-weight: bold;
  color: #2d3748;
  margin-bottom: 1rem;
}

.trainer-rating-large {
  margin-bottom: 1.5rem;
}

.stars {
  color: #ffd700;
  font-size: 1.5rem;
}

.rating-score {
  font-size: 1.5rem;
  font-weight: bold;
  color: #2d3748;
  margin-left: 0.5rem;
}

.review-count {
  color: #718096;
  margin-left: 0.5rem;
}

.trainer-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}

.specialty-tag-large {
  background: #e6fffa;
  color: #319795;
  padding: 0.5rem 1rem;
  border-radius: 20px;
  font-weight: 600;
}

.trainer-quick-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  gap: 1rem;
}

.stat-item {
  text-align: center;
  padding: 1rem;
  background: #f7fafc;
  border-radius: 10px;
}

.stat-label {
  display: block;
  color: #718096;
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
}

.stat-value {
  display: block;
  color: #2d3748;
  font-size: 1.2rem;
  font-weight: bold;
}

.trainer-actions-panel {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-width: 200px;
}

.btn {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 25px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
  text-align: center;
}

.btn-large {
  padding: 1rem 2rem;
  font-size: 1.1rem;
}

.btn-primary {
  background: #4299e1;
  color: white;
}

.btn-primary:hover {
  background: #3182ce;
  transform: translateY(-2px);
}

.btn-secondary {
  background: #48bb78;
  color: white;
}

.btn-secondary:hover {
  background: #38a169;
  transform: translateY(-2px);
}

.btn-outline {
  background: transparent;
  color: #4299e1;
  border: 2px solid #4299e1;
}

.btn-outline:hover {
  background: #4299e1;
  color: white;
  transform: translateY(-2px);
}

/* Tabs */
.content-tabs {
  background: white;
  border-radius: 15px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.1);
  overflow: hidden;
}

.tab-nav {
  display: flex;
  background: #f7fafc;
  border-bottom: 1px solid #e2e8f0;
}

.tab-btn {
  flex: 1;
  padding: 1rem 2rem;
  border: none;
  background: transparent;
  cursor: pointer;
  font-weight: 600;
  color: #718096;
  transition: all 0.3s ease;
}

.tab-btn.active {
  background: white;
  color: #4299e1;
  border-bottom: 3px solid #4299e1;
}

.tab-btn:hover {
  color: #4299e1;
}

.tab-content {
  padding: 2rem;
}

.tab-panel h3 {
  color: #2d3748;
  margin-bottom: 1rem;
  font-size: 1.3rem;
}

/* Profile Tab */
.bio-text {
  color: #4a5568;
  line-height: 1.6;
  margin-bottom: 2rem;
}

.background-info {
  margin-bottom: 2rem;
}

.info-item {
  margin-bottom: 0.75rem;
  color: #4a5568;
}

.certifications-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 1rem;
}

.cert-card {
  background: #f7fafc;
  padding: 1.5rem;
  border-radius: 10px;
  border-left: 4px solid #4299e1;
}

.cert-name {
  font-weight: bold;
  color: #2d3748;
  margin-bottom: 0.5rem;
}

.cert-date, .cert-org {
  color: #718096;
  font-size: 0.9rem;
  margin-bottom: 0.25rem;
}

/* Cases Tab */
.cases-grid {
  display: grid;
  gap: 2rem;
}

.case-card {
  background: #f7fafc;
  padding: 1.5rem;
  border-radius: 10px;
}

.case-images {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
  justify-content: center;
}

.case-image {
  width: 120px;
  height: 120px;
  border-radius: 10px;
  object-fit: cover;
}

.arrow {
  font-size: 2rem;
  color: #4299e1;
  font-weight: bold;
}

.case-info h4 {
  color: #2d3748;
  margin-bottom: 0.5rem;
}

.case-description {
  color: #4a5568;
  margin-bottom: 1rem;
  line-height: 1.6;
}

.case-stats {
  display: flex;
  gap: 2rem;
  color: #718096;
  font-size: 0.9rem;
}

/* Style Tab */
.style-description p {
  color: #4a5568;
  line-height: 1.6;
  margin-bottom: 2rem;
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  margin-bottom: 2rem;
}

.feature-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem;
  background: #f7fafc;
  border-radius: 8px;
}

.feature-icon {
  color: #48bb78;
  font-weight: bold;
}

.feature-text {
  color: #4a5568;
}

.target-audience {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.audience-tag {
  background: #e6fffa;
  color: #319795;
  padding: 0.5rem 1rem;
  border-radius: 20px;
  font-weight: 600;
}

/* Schedule Tab */
.calendar-widget {
  background: #f7fafc;
  border-radius: 10px;
  padding: 1rem;
}

.calendar-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.nav-btn {
  background: #4299e1;
  color: white;
  border: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.2rem;
}

.week-range {
  font-weight: bold;
  color: #2d3748;
}

.calendar-grid {
  display: grid;
  grid-template-columns: 80px repeat(7, 1fr);
  gap: 1px;
  background: #e2e8f0;
  border-radius: 8px;
  overflow: hidden;
}

.day-header {
  background: #4299e1;
  color: white;
  padding: 0.75rem 0.5rem;
  text-align: center;
  font-weight: 600;
  font-size: 0.9rem;
}

.time-slot {
  background: #4299e1;
  color: white;
  padding: 0.75rem 0.5rem;
  text-align: center;
  font-weight: 600;
  font-size: 0.8rem;
}

.day-slots {
  display: contents;
}

.slot {
  background: white;
  padding: 0.5rem;
  text-align: center;
  cursor: pointer;
  font-size: 0.8rem;
  transition: background 0.3s ease;
}

.slot.available {
  background: #c6f6d5;
  color: #22543d;
}

.slot.available:hover {
  background: #9ae6b4;
}

.slot.booked {
  background: #fed7d7;
  color: #742a2a;
  cursor: not-allowed;
}

.slot.unavailable {
  background: #e2e8f0;
  color: #a0aec0;
  cursor: not-allowed;
}

/* Reviews Tab */
.reviews-summary {
  margin-bottom: 2rem;
}

.rating-overview {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2rem;
  align-items: center;
}

.overall-rating {
  text-align: center;
}

.rating-number {
  font-size: 3rem;
  font-weight: bold;
  color: #2d3748;
  display: block;
}

.rating-stars {
  color: #ffd700;
  font-size: 1.5rem;
  margin: 0.5rem 0;
}

.rating-count {
  color: #718096;
}

.rating-breakdown {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.rating-bar {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.stars-label {
  min-width: 40px;
  color: #4a5568;
  font-size: 0.9rem;
}

.bar-container {
  flex: 1;
  height: 8px;
  background: #e2e8f0;
  border-radius: 4px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  background: #ffd700;
  transition: width 0.3s ease;
}

.count {
  min-width: 30px;
  color: #718096;
  font-size: 0.9rem;
}

.reviews-list {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.review-card {
  background: #f7fafc;
  padding: 1.5rem;
  border-radius: 10px;
}

.review-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1rem;
}

.reviewer-info {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.reviewer-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}

.reviewer-name {
  font-weight: 600;
  color: #2d3748;
}

.review-date {
  color: #718096;
  font-size: 0.9rem;
}

.review-rating {
  color: #ffd700;
  font-size: 1.2rem;
}

.review-content p {
  color: #4a5568;
  line-height: 1.6;
  margin-bottom: 1rem;
}

.review-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.review-tag {
  background: #e6fffa;
  color: #319795;
  padding: 0.25rem 0.75rem;
  border-radius: 15px;
  font-size: 0.8rem;
  font-weight: 600;
}

/* Responsive Design */
@media (max-width: 768px) {
  .trainer-header {
    grid-template-columns: 1fr;
    text-align: center;
  }
  
  .trainer-basic-info {
    flex-direction: column;
    align-items: center;
  }
  
  .trainer-actions-panel {
    align-self: center;
  }
  
  .tab-nav {
    flex-wrap: wrap;
  }
  
  .tab-btn {
    flex: none;
    min-width: 120px;
  }
  
  .calendar-grid {
    grid-template-columns: 60px repeat(7, 1fr);
  }
  
  .rating-overview {
    grid-template-columns: 1fr;
    text-align: center;
  }
}
</style>